import { RegisterComponent } from '@feng3d/ecs';
import { Mesh3D, Node3D } from 'feng3d-three';
import { Brush } from '../imports';

declare module '@feng3d/ecs'
{
    interface ComponentMap { Brush3D: Brush3D; }
}

/**
 * 包装`three-bvh-csg`中`Brush`
 */
@RegisterComponent({ name: 'Brush3D' })
export class Brush3D extends Mesh3D
{
    declare _mesh: Brush;

    init()
    {
        super.init();

        const brush = new Brush(this.geometry?._geometry, this.material?._material);
        this._set(brush);

        this._entity.isBrush = true;
    }

    destroy(): void
    {
        super.destroy();

        this._entity.isBrush = false;
    }

    static get(brush: Brush)
    {
        let mesh3D = this._map.get(brush) as Brush3D;
        if (!mesh3D)
        {
            mesh3D = new Node3D().addComponent('Brush3D');
            //
            mesh3D._set(brush);
            //
            this._map.set(brush, mesh3D);
        }

        return mesh3D;
    }
}

